module.exports = ({ app }) => {
  return async function verify(ctx, next) {
    // console.log(ctx.request);
    const token = ctx.request.header.authorization.replace("Bearer ", "");
    try {
      let res = await app.jwt.verify(token, app.config.jwt.secret);
      // console.log("中间件获取token信息", res);
      ctx.state.userId = res.id;
      // ctx.state.email = res.email;
      await next();
    } catch (error) {
      console.log(error);
      if (error.name === "TokenExpiredError") {
        ctx.state.userId = "";
        // ctx.state.email = "";
        return (ctx.body = {
          code: -666,
          message: "token过期了,请重新登录",
        });
      }
    }
  };
};
